<template>
    <el-drawer :model-value="modelValue" direction="rtl" :close-on-press-escape="false" size="645px"
        :close-on-click-modal="false" :before-close="beforeClose">
        <template #title>
            <h4>合同信息</h4>
        </template>
        <template #default>
            <CostEntryFrom v-if="modelValue" ref="costEntryRef" :apply-order-id="props.premisesData.applyOrderId"
                :premise-id="props.premisesData.premisesId" :is-edit="isEditItem" :show-more="true"></CostEntryFrom>
        </template>
        <template #footer>
            <div style="flex: auto;">
                <el-button size="large" @click="beforeClose"> 取消 </el-button>
                <!-- <el-button v-if="!isEditItem" size="large" type="primary" @click="changeEditItem"> 编辑 </el-button> -->
                <el-button size="large" type="primary" @click="submitForm"> 确认 </el-button>
            </div>
        </template>
    </el-drawer>
</template>

<script lang="ts" setup>
import { reactive, ref, defineEmits, computed } from 'vue';
import CostEntryFrom from './CostEntryFrom.vue';
import { ElMessage } from 'element-plus';
import { updatePremisesContract, addPremisesContract } from '@/api/houseManage';
import { useStore } from 'vuex';

const emits = defineEmits<{ (e: 'update:modelValue', visible: boolean): void; (e: 'success'): void }>();

const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false,
    },
    premisesData: {
        type: Object,
        default: () => {
            return {};
        },
    },
});
const store = useStore();
const costEntryRef: any = ref<FormInstance>();
const isEditItem = computed(() => store.state.plan.isEdit);
const beforeClose = () => {
    emits('update:modelValue', false);
};
const submitForm = async () => {

    try {
        let formList = await costEntryRef.value.submitForm();
        console.log('1246');
        let isEdit = !!formList.find((item: any) => item.id);
        if (isEdit) {
            await updatePremisesContract(formList);
        } else {
            await addPremisesContract(formList);
        }
        ElMessage.success('操作成功');
        beforeClose();
    } catch (error) {
        console.log(error);
    }




};
const changeEditItem = () => {
    store.commit('plan/setIsEdit', true);
};
</script>

<style lang="scss" scoped></style>
